package com.diamond.web.utils;

import java.io.IOException;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;

public class SMSYunPianUtils {
	static Logger log = Logger.getLogger(SMSYunPianUtils.class);
	private static final String API_KEY = "f038943ede867eaabbf87bbd797d4243 ";
	private static final String API_URL="https://sms.yunpian.com/v1/sms/send.json";
	public static final String YILIDA_SIGN="【一里达】";
	public static final String SMS_TMPL_CODE = "您的验证码是%s。如非本人操作，请忽略本短信";
	public static final String SMS_TEMP_PAY_NOFITY="您好，您的订单\"%s\"，已维修完成，付款地址为:%s";
	
	public static JSONObject sendCode(String code,String mobile) throws IOException{
		return sendCode(YILIDA_SIGN, SMS_TMPL_CODE,code,mobile);
	}
	
	public static JSONObject sendCode(String sign,String tmpl,String code,String mobile) throws IOException{
		String text = String.format(tmpl, code);
		return sendMsg(sign+text,mobile);
	}
	
	public static JSONObject sendPayNofity(String mobile,String orderNumber,String payUrl) throws IOException{
		String text = YILIDA_SIGN+String.format(SMS_TEMP_PAY_NOFITY, orderNumber,payUrl);
		return sendMsg(text, mobile);
	}
	
	public static JSONObject sendMsg(String text,String mobile) throws IOException{
		String params = String.format("apikey=%s&mobile=%s&text=%s", API_KEY,mobile,text);
		String result = HttpUtils.sendPost(API_URL, params,"utf-8");
		log.info("发送短信到信:"+mobile);
		log.info("文本:"+text);
		log.info("短信发送结果:"+result);
		JSONObject object = new JSONObject(result);
		object.put("content", text);
		return object;
	}
	
	public static void main(String[] args) {
		try {
			String random = RandomStringUtils.random(4, false, true);
			sendCode(random,"13795462311");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
